package com.scm.service.impl;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

import com.scm.common.core.domain.entity.SysUser;
import com.scm.dto.req.supplier.ScmSupplierProjectReq;
import com.scm.dto.req.supplier.ScmSupplierReq;
import com.scm.dto.resp.supplier.SupplierProjectResp;
import com.scm.persist.model.ScmProductProject;
import com.scm.persist.model.ScmProductProjectExample;
import com.scm.persist.model.ScmSupplier;
import com.scm.persist.model.ScmSupplierExample;
import com.scm.persist.mybatis.ScmProductProjectMapper;
import com.scm.persist.mybatis.ScmSupplierMapper;
import com.scm.service.IScmSupplierService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 订单商品Service业务层处理
 *
 * @author reptilemanager
 * @date 2024-04-20
 */
@Service
public class ScmSupplierServiceImpl implements IScmSupplierService
{
    @Autowired
    private ScmSupplierMapper scmSupplierMapper;

    @Autowired
    private ScmProductProjectMapper scmProductProjectMapper;

    public static ScmProductProject toScmProductProject(ScmSupplierProjectReq scmSupplierProjectReq) {
        if (scmSupplierProjectReq == null) {
            return null;
        }
        ScmProductProject scmProductProject = new ScmProductProject();
        scmProductProject.setProjectId(scmSupplierProjectReq.getProjectId());
        scmProductProject.setProjectName(scmSupplierProjectReq.getProjectName());
        scmProductProject.setContactPerson(scmSupplierProjectReq.getContactPerson());
        scmProductProject.setTel(scmSupplierProjectReq.getTel());
        scmProductProject.setEmail(scmSupplierProjectReq.getEmail());
        scmProductProject.setRemark(scmSupplierProjectReq.getRemark());
        scmProductProject.setProjectRate(scmSupplierProjectReq.getProjectRate());

        // Not mapped ScmProductProject fields:
        // id
        // product
        // tenantId
        // createTime
        // createUser
        // updateTime
        // updateUser
        return scmProductProject;
    }

    public static SupplierProjectResp toSupplierProjectResp(ScmProductProject scmProductProject) {
        if (scmProductProject == null) {
            return null;
        }
        SupplierProjectResp supplierProjectResp = new SupplierProjectResp();
        supplierProjectResp.setProjectId(scmProductProject.getProjectId());
        supplierProjectResp.setProjectName(scmProductProject.getProjectName());
        supplierProjectResp.setContactPerson(scmProductProject.getContactPerson());
        supplierProjectResp.setTel(scmProductProject.getTel());
        supplierProjectResp.setEmail(scmProductProject.getEmail());
        supplierProjectResp.setRemark(scmProductProject.getRemark());
        supplierProjectResp.setProjectRate(scmProductProject.getProjectRate());

        // Not mapped ScmProductProject fields:
        // id
        // product
        // tenantId
        // createTime
        // createUser
        // updateTime
        // updateUser
        return supplierProjectResp;
    }


    /**
     * 查询订单商品
     *
     * @param id 订单商品主键
     * @return 订单商品
     */
    @Override
    public ScmSupplier selectScmSupplierById(Long id)
    {

        return scmSupplierMapper.selectByPrimaryKey(id);
    }

    /**
     * 查询订单商品列表
     *
     * @param scmSupplier 订单商品
     * @return 订单商品
     */
    @Override
    public List<ScmSupplier> selectScmSupplierList(SysUser sysUser,ScmSupplier scmSupplier)
    {
        ScmSupplierExample example = new ScmSupplierExample();
        ScmSupplierExample.Criteria criteria = example.createCriteria().andTenantIdEqualTo(sysUser.getTenantId());
        return scmSupplierMapper.selectByExample(example);
    }

    /**
     * 新增订单商品
     *
     * @param scmSupplier 订单商品
     * @return 结果
     */
    @Override
    public int insertScmSupplier(ScmSupplier scmSupplier,SysUser sysUser )
    {
        scmSupplier.setCreateTime(System.currentTimeMillis());
        scmSupplier.setCreateUser(sysUser.getUserName());
        scmSupplier.setStatus("1");
        scmSupplier.setTenantId(sysUser.getTenantId());
        return scmSupplierMapper.insert(scmSupplier);
    }


    @Override
    public int insertScmSupplier(ScmSupplierReq req, SysUser sysUser) {
        ScmSupplier scmSupplier = new ScmSupplier();
        // 复制req 属性到 scmSupplier
        scmSupplier.setId(null);
        scmSupplier.setName(req.getName());
        scmSupplier.setContactPerson(req.getContactPerson());
        scmSupplier.setTel(req.getTel());
        scmSupplier.setEmail(req.getEmail());
        scmSupplier.setRemark(req.getRemark());
        insertScmSupplier(scmSupplier, sysUser);

        List<ScmSupplierProjectReq> projectReqList = req.getProjectReqList();

        ScmProductProjectExample example = new ScmProductProjectExample();
        ScmProductProjectExample.Criteria criteria = example.createCriteria();
        criteria.andProjectIdEqualTo(req.getId());
        scmProductProjectMapper.deleteByExample(example);
        if(CollectionUtils.isNotEmpty(projectReqList)){
            for (ScmSupplierProjectReq scmSupplierProjectReq : projectReqList) {
                scmSupplierProjectReq.getProjectId();
                ScmProductProject scmProductProject1 = toScmProductProject(scmSupplierProjectReq);
                scmProductProject1.setTenantId(sysUser.getTenantId());
                scmProductProject1.setProduct(scmSupplier.getId());
                scmProductProject1.setCreateTime(System.currentTimeMillis());
                scmProductProject1.setCreateUser(sysUser.getUserName());
                scmProductProjectMapper.insert(scmProductProject1);
            }
        }

        return 1;
    }

    /**
     * 修改订单商品
     *
     * @param scmSupplier 订单商品
     * @return 结果
     */
    @Override
    public int updateScmSupplier(ScmSupplier scmSupplier, SysUser sysUser)
    {
        return scmSupplierMapper.updateByPrimaryKeySelective(scmSupplier);
    }


    @Override
    public int updateScmSupplier(ScmSupplierReq req, SysUser sysUser) {
        ScmSupplier scmSupplier = new ScmSupplier();
        // 复制req 属性到 scmSupplier
        scmSupplier.setId(req.getId());
        scmSupplier.setName(req.getName());
        scmSupplier.setContactPerson(req.getContactPerson());
        scmSupplier.setTel(req.getTel());
        scmSupplier.setEmail(req.getEmail());
        scmSupplier.setRemark(req.getRemark());
        updateScmSupplier(scmSupplier, sysUser);

        List<ScmSupplierProjectReq> projectReqList = req.getProjectReqList();
        ScmProductProjectExample example = new ScmProductProjectExample();
        ScmProductProjectExample.Criteria criteria = example.createCriteria();
        criteria.andProductEqualTo(req.getId());
        scmProductProjectMapper.deleteByExample(example);
        if(CollectionUtils.isNotEmpty(projectReqList)){
            for (ScmSupplierProjectReq scmSupplierProjectReq : projectReqList) {
                 scmSupplierProjectReq.getProjectId();
                ScmProductProject scmProductProject1 = toScmProductProject(scmSupplierProjectReq);
                scmProductProject1.setTenantId(sysUser.getTenantId());
                scmProductProject1.setProduct(req.getId());
                scmProductProject1.setCreateTime(System.currentTimeMillis());
                scmProductProject1.setCreateUser(sysUser.getUserName());
                scmProductProjectMapper.insert(scmProductProject1);
            }
        }


        return 1;
    }



    /**
     * 批量删除订单商品
     *
     * @param ids 需要删除的订单商品主键
     * @return 结果
     */
    @Override
    public int deleteScmSupplierByIds(List<Long> ids)
    {
        if(CollectionUtils.isEmpty(ids)){

            return 0;
        }
        ScmSupplierExample example = new ScmSupplierExample();
        ScmSupplierExample.Criteria criteria = example.createCriteria();
        criteria.andIdIn(ids);

        ScmSupplier record = new ScmSupplier();
        record.setStatus("0");
        //scmSupplierMapper.deleteByExample(example);
        return scmSupplierMapper.updateByExampleSelective(record, example);
    }

    /**
     * 删除订单商品信息
     *
     * @param id 订单商品主键
     * @return 结果
     */
    @Override
    public int deleteScmSupplierById(Long id)
    {
        return scmSupplierMapper.deleteByPrimaryKey(id);
    }


    @Override
    public List<SupplierProjectResp> selectScmSupplierProjectList(Long tenantId, Long supplierId) {
        ScmProductProjectExample example = new ScmProductProjectExample();
        ScmProductProjectExample.Criteria criteria = example.createCriteria();
        criteria.andProductEqualTo(supplierId);
        List<ScmProductProject> scmProductProjects = scmProductProjectMapper.selectByExample(example);
        if(CollectionUtils.isNotEmpty(scmProductProjects)){
           return scmProductProjects.stream().map(ScmSupplierServiceImpl::toSupplierProjectResp).collect(Collectors.toList());
        }
        return Collections.emptyList();
    }
}
